package com.zut.controller;


import com.zut.pojo.Admin;
import com.zut.pojo.Result;
import com.zut.pojo.Type;
import com.zut.service.AdminService;
import com.zut.util.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

@RestController
@CrossOrigin
public class AdminController {

    @Autowired
    private AdminService adminService;
    @GetMapping("/adminLogin")
    public Result AdminLogin(@RequestParam("name") String name, @RequestParam("password") String password){
        Admin admin = adminService.login(name,password);
        if (admin != null){
            Map<String,Object> claims = new HashMap<>();
            claims.put("name",admin.getName());
            claims.put("password",admin.getPassword());
            String s = JwtUtils.generateJwt(claims);
            ArrayList<String> list = new ArrayList<>();
            list.add(admin.getName());
            list.add(s);
            return Result.success(list);
        }else {
            return Result.error("用户名或密码错误！！！");
        }
    }

    // 添加类型
    @PostMapping("/addType")
    public Result addType(@RequestBody Type type){
        Integer i=adminService.addType(type.getName(),type.getDetail());
        if(i!=0){
            return Result.success();
        }else {
            return Result.error("类型添加失败！！！");
        }
    }
}
